package customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import customer.zspt.cn.customer.R;

/**
 * Created by Administrator on 2018/8/16 0016.
 */

public class RadioBoxView extends BaseView {
    private  TextView title;
    private  String[] optionsStr;
    private  TextView choose_Tv;
    private ArrayList list;
    private int chooseIndex = 0;
    String result ;
    public RadioBoxView(final Context context, String titleVaules, JSONArray options, String selected) {
        super(context,titleVaules);
        View.inflate(context, R.layout.chat_order_content_checkbox,this);
        title = (TextView) findViewById(R.id.title);
        title.setText(titleVaules);
        choose_Tv = (TextView) findViewById(R.id.choose_Tv);
        choose_Tv.setText(selected);
        choose_Tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(context);
            }
        });
        optionsStr = new String[options.length()];
        for(int i=0;i<options.length();i++){
            try {
                JSONObject object = (JSONObject) options.get(i);
                optionsStr[i] = object.getString("label");
                if(selected.equals(object.getString("label"))){
                    chooseIndex = i;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    /**弹出时间选择框*/
    private void showDialog(Context context) {
        list = new ArrayList();
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("单项选项选择");
        dialog.setSingleChoiceItems(optionsStr, chooseIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chooseIndex = which;
            }
        });
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                result = optionsStr[chooseIndex];
                choose_Tv.setText(optionsStr[chooseIndex]+">");
            }
        });
        dialog.show();
    }
    @Override
    public String result() {
        return result;
    }
}
